<?php

class QuestionController extends Controller
{

    /**
     * 风险评估入口，根据疾病做相应处理
     * @param $disid 疾病种类，默认高血压
     */
//	public $layout = 'application.views.layouts.mobile';
    public $layout = "//layouts/disease-mobile";

    public function actionTemproray()
    {
        $this->render('question');
    }

    public function actionAll()
    {
        $uid = (isset($_GET["uid"])&&$_GET['uid'] > 0) ? $_GET["uid"] : '';

        if (isset($_POST['Q'])) {
            $question_str = $_POST['Q'];
            $question_arr = array();
            for ($i = 1; $i < 18; $i++) {
                if (isset($question_str[$i]["ans"])) {
                    $question_arr[$i] = $question_str[$i];
                } else {
                    $question_arr[$i] = array("ans" => array());
                }
            }

            //如果是登陆用户并且有体检报告信息
            //否则放入session中
            if (!empty($uid)) {
                $ret = EvaluateQuestionnaire::model()->saveQAnswer($uid, $question_arr);
                if ($ret === false) {
                    //$this->redirect($this->createUrl("app/disease/evaluatelistshow",array("uid"=>$uid)));
                    throw new CHttpException(200, "保存问卷答案出错");
                }

                if(isset($_POST['R'])){
                    $m = new ReportService();
                    $m->saveQuestionItems($uid, $_POST['R']);
                }else{
                    throw new CHttpException(200, "保存问卷答案出错");
                }
            } else {
//                echo "<pre>";var_dump($question_arr);echo "</pre>";exit();
                Yii::app()->session['question_arr'] = $question_arr;
//                $ret = EvaluateQuestionnaire::model()->convertQAnswer($uid, $question_arr);
//                throw new CHttpException(200, "用户未登录");
            }
            $this->redirect($this->createUrl("app/disease/evaluatelistshow", array("uid" => $uid)));
        }


        $ret = EvaluateQuestionnaire::model()->findAllByAttributes(array("questype" => array(1, 2, 3, 4)),
            array("order" => "questype"));
        if (count($ret) == 0) {
            throw new CHttpException(200, '系统开小差啦', JSONHelper::ERROR_SERVICE);
        }
        $ques = array();
        foreach ($ret as $it) {
            if (!isset($ques[$it->questype])) {
                $ques[$it->questype] = array();
            }
            $q["id"] = $it->id;
            $q["title"] = $it->title;
            array_push($ques[$it->questype], $q);
        }

        //体检数据
        if (!empty($uid)) {
            $m = new ReportService();
            $reportdata = $m->getDiseaseReportTemplate();
            $items = array();
            foreach ($reportdata as $key => $it) {
                $item["field_name"] = $key;
                $item["name"] = $it["name"];
                array_push($items, $item);
            }
            if (count($items)) {
                $ret = $m->getValueByEles($uid, $items);
                foreach ($reportdata as $key => $it) {
                    isset($ret[$key]) ? $reportdata[$key] = $ret[$key] : "";
                }
            }
            $this->renderPartial("all", array("ques" => $ques, "reportdata" => $reportdata));
        }else{
            $this->renderPartial("all", array("ques" => $ques));
        }
    }

    public function actionQuestion($ques_str, $token, $disid, $uid)
    { //
        $ques_arr = explode(",", $ques_str);
        /* 1 get the quesinfo order by questype
         * 2	get the view of the quesinfo to array(questype,array(ques))
         */
        $ques_ins = new EvaluateQuestionnaire();
        $ques_ret = $ques_ins->getQuesinfo($ques_arr);

        $ques_view_arr = array();
        $ques_type_arr = array();

        $questype = $ques_ret[0]->questype;
        foreach ($ques_ret as $quesinfo) {
            if ($quesinfo->questype == $questype) {
                $view = $this->renderPartial($quesinfo->view, array("que" => $quesinfo, "pagecount" => 1), true);
                array_push($ques_view_arr, $view);
            } else {
                array_push($ques_type_arr, array("questype" => $questype, "quesview" => $ques_view_arr));
                $questype = $quesinfo->questype;
                $ques_view_arr = array();

                $view = $this->renderPartial($quesinfo->view, array("que" => $quesinfo, "pagecount" => 1), true);
                array_push($ques_view_arr, $view);
            }
        }
        array_push($ques_type_arr, array("questype" => $questype, "quesview" => $ques_view_arr));


        $this->render("question", array("uid" => $uid, "token" => $token, "ques" => $ques_type_arr, "disid" => $disid, "distype" => $this->getDisType($disid)));
    }

    /**
     * save the answer
     * Enter description here ...
     */
    public function actionGetresult($disid, $uid, $token)
    {
        $q = new EvaluateQuestionnaire();
        $question_str = $_POST['Q'];
        /* $ques = Yii::app()->session["ques"];
          $count = count($ques); */
        $question_arr = array();

        $size = count(EvaluateQuestionnaire::model()->findAll());
        for ($i = 1; $i < 17; $i++) {
            if (isset($question_str[$i]["ans"])) {
                $question_arr[$i] = $question_str[$i];
            } else {
                $question_arr[$i] = array("ans" => array());
            }
        }

//		$hum_health_ins = new EvaluateHumanHealthTag();
//		$hum_health_ins->clearTabs($uid, 'badtags');
        //        $uid = Yii::app()->session["user"];
        $q->saveQAnswer($uid, $question_arr);
        $this->redirect(array("app/disease/analysis", "disid" => $disid, "uid" => $uid, "token" => $token));
//		$this->redirect(array("suggestion/analysis", "disid" =>$disid, "uid" => $uid,"token"=>$token));
    }

    public function actionIndex($id)
    {
        $ques = Yii::app()->session["ques"];
        $ret = EvaluateQuestionnaire::model()->findByPk($id);
        $this->render($ret->view, array("ques" => $ques, "que" => $ret, "current" => $id, "distype" => $this->getDisType()));
    }

    private function getDisType($disid)
    {
        $type = "";
        switch ($disid) {
            case 1:
                $type = "高血压";
                break;
            case 2:
                $type = "冠心病";
                break;
            case 3:
                $type = "脑卒中";
                break;
            case 4:
                $type = "糖尿病";
                break;
            default:
                $type = "未知";
                break;
        }
        return $type;
    }

}
